//
//  CSGetTrueDay.m
//  classroom
//
//  Created by Macx on 16/10/28.
//  Copyright © 2016年 LiuZhuo. All rights reserved.
//

#import "CSGetTrueDay.h"
@interface CSGetTrueDay()
@property (nonatomic,assign) long  dijizhou ;
@property (nonatomic,assign) long  zhouji ;
@end
@implementation CSGetTrueDay
-(NSString * )dateName:(NSString *)dateName{
     NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
      NSDate * oldDate = [userDefaults objectForKey:@"date"];
    DDLogDebug(@"%@",oldDate);
    NSDate *  nowDate=[NSDate date];
    DDLogDebug(@"%@",nowDate);
    NSInteger  dateFrom = [self getDaysFrom:oldDate To:nowDate];
    
    long we = dateFrom/7;
    long da = dateFrom%7;
    if ( [self isPureInt:[dateName substringWithRange:NSMakeRange(1, 2)]] ) {
        self.dijizhou = [[dateName substringWithRange:NSMakeRange(1, 2)] integerValue];
        self.zhouji  =  [[CSTransAlabo translation:[dateName substringWithRange:NSMakeRange(6, 1)]]  integerValue];
        
    }else{
        self.dijizhou = [[dateName substringWithRange:NSMakeRange(1, 1)] integerValue];
        self.zhouji  =  [[CSTransAlabo translation:[dateName substringWithRange:NSMakeRange(5, 1)]]  integerValue];
        
    }
    
    long nowDayAdd = self.zhouji+da;
    if (nowDayAdd>7) {
        nowDayAdd = nowDayAdd-7;
        we +=1;
    }
    
    NSString * nowDay = [NSString stringWithFormat:@"%ld",nowDayAdd];
    
   return  [NSString stringWithFormat:@"第%ld周 周%@",self.dijizhou+we,[CSTransChinese translation:nowDay]];
    
    
}
-(NSInteger)getDaysFrom:(NSDate *)serverDate To:(NSDate *)endDate
{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 每周是从星期一开始的
    
    [gregorian setFirstWeekday:2];
    
    //去掉时分秒信息
    NSDate *fromDate;
    NSDate *toDate;
    [gregorian rangeOfUnit:NSCalendarUnitDay startDate:&fromDate interval:NULL forDate:serverDate];
    [gregorian rangeOfUnit:NSCalendarUnitDay startDate:&toDate interval:NULL forDate:endDate];
    DDLogDebug(@"%@-----%@",fromDate,toDate);
    NSDateComponents *dayComponents = [gregorian components:NSCalendarUnitDay fromDate:fromDate toDate:toDate options:0];
     DDLogDebug(@"%ld--------",(long)dayComponents.day);
    return dayComponents.day;
   
}
// 判断字符串是否为整形数字
- (BOOL)isPureInt:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}
@end
